g = (x * x for x in range(10))


def fibonacci(n):
    index, a, b = 0, 0, 1
    while index < n:
        yield a
        a, b = b, a + b
        index = index + 1
    return 'done'


def next_iter():
    g1 = fibonacci(6)
    print(next(g1))
    print(next(g1))
    print(next(g1))
    print(next(g1))
    print(next(g1))
    print(next(g1))
    try:
        print(next(g1))  # 第11次调用会报错
    except StopIteration as e:
        print("generator return value:", e.value)


def for_iter():
    g1 = fibonacci(10)
    for i in g1:
        print(i)


if __name__ == '__main__':
    # for_iter()
    next_iter()
